# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_vip.entity.vo.bg_vip_vo import BgVipPageModel, BgVipModel
from module_vip.service.bg_vip_service import BgVipService

bgVipController = APIRouter(prefix='/bg/vip', dependencies=[Depends(LoginService.get_current_user)])


@bgVipController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:query'))])
async def get_bg_vip_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgVipPageModel = Depends( BgVipPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('BgVip'))
):
    bg_vip_result = await BgVipService.get_bg_vip_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=bg_vip_result)


@bgVipController.get('/userVip', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:query'))])
async def get_bg_user_vip(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgVip')),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    bg_vip_result = await BgVipService.get_bg_vip_by_user_id(query_db, current_user.user.user_id)
    return ResponseUtil.success(data=bg_vip_result)

@bgVipController.get('/getById/{bgVipId}', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:query'))])
async def get_bg_vip_by_id(
        request: Request,
        bgVipId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgVip'))
):
    bg_vip = await BgVipService.get_bg_vip_by_id(query_db, bgVipId)
    return ResponseUtil.success(data=bg_vip)

@bgVipController.get('/statistics', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:query'))])
async def get_vip_statistics(
        request: Request,
        query_db: AsyncSession = Depends(get_db)
):
    """
    获取VIP会员统计信息
    """
    statistics = await BgVipService.get_vip_statistics(query_db)
    return ResponseUtil.success(data=statistics)


@bgVipController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:add'))])
@Log(title='bg_vip', business_type=BusinessType.INSERT)
async def add_bg_vip (
    request: Request,
    add_model: BgVipModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await BgVipService.add_bg_vip(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@bgVipController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:edit'))])
@Log(title='bg_vip', business_type=BusinessType.UPDATE)
async def update_bg_vip(
    request: Request,
    edit_model: BgVipModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgVipService.update_bg_vip(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgVipController.delete('/delete/{bgVipIds}', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:remove'))])
@Log(title='bg_vip', business_type=BusinessType.DELETE)
async def del_bg_vip(
    request: Request,
    bgVipIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgVipIds.split(',')
    del_result = await BgVipService.del_bg_vip(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgVipController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:export'))])
@Log(title='bg_vip', business_type=BusinessType.EXPORT)
async def export_bg_vip(
    request: Request,
    bg_vip_form: BgVipPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgVip')),
):
    # 获取全量数据
    export_result = await BgVipService.export_bg_vip_list(
        query_db, bg_vip_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgVipController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('bg:vip:import'))])
async def import_bg_vip(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()